//
//  PhotosView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/2.
//

static NSInteger photosMaxCol = 3;
static CGFloat   maxWidth;
static CGFloat   photoMargin;
static CGFloat   photoW;
static CGFloat   photoH;

#import "PhotosView.h"
#import "ShopModel.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>

@implementation PhotosView

+ (PhotosView *)photosViewWithWidth:(CGFloat)width margin:(CGFloat)margin {
    return [[self alloc] initWithWidth:width margin:margin];
}

- (instancetype)initWithWidth:(CGFloat)width margin:(CGFloat)margin {
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        maxWidth = width;
        photoMargin = margin;
        photoW = (width - (photosMaxCol - 1) * margin) / photosMaxCol;
        photoH = photoW;
    }
    return self;
}

- (void)updateWidth:(CGFloat)width {
    maxWidth = width;
    photoW = (width - (photosMaxCol - 1) * photoMargin) / photosMaxCol;
    photoH = photoW;
    [self setNeedsLayout];
    [self layoutSubviews];
}

- (void)setPhotos:(NSArray *)photos {
    _photos = photos;
    if (self.subviews.count > 0) {
        return;
    }
    for (int i = 0; i < photos.count; i++) {
        UIImageView *imgV = [[UIImageView alloc] init];
        imgV.tag = i;
        imgV.contentMode = UIViewContentModeScaleAspectFill;
        imgV.clipsToBounds = YES;
        imgV.userInteractionEnabled = YES;
        [self addSubview:imgV];
        [imgV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)]];
        
        NSString *url = photos[i];
        [imgV sd_setImageWithURL:[NSURL URLWithString:url]];
    }
}

- (void)setImages:(NSArray *)images {
    _images = images;
    if (self.subviews.count > 0) {
        for (UIView *v in self.subviews) {
            [v removeFromSuperview];
        }
    }
    for (int i = 0; i < images.count; i++) {
        UIImageView *imgV = [[UIImageView alloc] init];
        imgV.tag = i;
        imgV.contentMode = UIViewContentModeScaleAspectFill;
        imgV.clipsToBounds = YES;
        imgV.layer.cornerRadius = 8;
        imgV.userInteractionEnabled = YES;
        [self addSubview:imgV];
        [imgV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)]];
        
        ShopModelImageModel *model = images[i];
        [imgV sd_setImageWithURL:[NSURL URLWithString:model.imgThumbnailUrl.length > 0 ? model.imgThumbnailUrl : model.imgUrl] placeholderImage:[UIImage imageNamed:@"shopD"]];
        if (model.isVideo) {
            UIImageView *playImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_play"]];
            [imgV addSubview:playImageV];
            playImageV.bounds = CGRectMake(0, 0, 30, 30);
            playImageV.center = imgV.center;
        }
    }
}

- (void)setPhototImages:(NSArray *)phototImages {
    _phototImages = phototImages;
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i = 0; i < phototImages.count; i++) {
        UIImageView *imgV = [[UIImageView alloc] init];
        imgV.tag = i;
        imgV.contentMode = UIViewContentModeScaleAspectFill;
        imgV.clipsToBounds = YES;
        imgV.userInteractionEnabled = YES;
        [self addSubview:imgV];
        [imgV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)]];
        imgV.image = phototImages[i];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    photoW = (maxWidth - (photosMaxCol - 1) * photoMargin) / photosMaxCol;
    photoH = photoW;
    
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    CGFloat w = photoW;
    CGFloat h = photoH;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger maxCol = photosMaxCol;
        NSInteger col = idx % maxCol;
        NSInteger row = idx / maxCol;
        x = col * (photoW + photoMargin);
        y = row * (photoH + photoMargin);
        obj.frame = CGRectMake(x, y, w, h);
        /// 如果有播放按钮
        if (obj.subviews.count > 0) {
            [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
                obj1.center = CGPointMake(w/2, h/2);
            }];
        }
    }];
}

- (void)imageClick:(UIGestureRecognizer *)sender {
    if ([self.delegate respondsToSelector:@selector(photosTaped:)]) {
        [self.delegate photosTaped:(UIImageView *)sender.view];
    }
}

+ (CGSize)sizeWithCount:(NSInteger)count {
    CGFloat photosW = 0;
    CGFloat photosH = 0;
    NSInteger cols = photosMaxCol;
    NSInteger rows = (count + cols - 1) / cols;
    photosW = photoW * cols + (cols - 1) * photoMargin;
    photosH = photoH * rows + (rows - 1) * photoMargin;
    return CGSizeMake(photosW, photosH);
}

+ (CGSize)sizeWithCount:(NSInteger)count width:(CGFloat)width margin:(CGFloat)margin {
    CGFloat photoW = (width - (photosMaxCol - 1) * margin) / photosMaxCol;
    CGFloat photoH = photoW;
    
    CGFloat photosW = 0;
    CGFloat photosH = 0;
    NSInteger cols = photosMaxCol;
    NSInteger rows = (count + cols - 1) / cols;
    
    photosW = photoW * cols + (cols - 1) * margin;
    photosH = photoH * rows + (rows - 1) * margin;
    return CGSizeMake(photosW, photosH);
}

+ (CGSize)sizeWithImages:(NSArray *)images width:(CGFloat)width margin:(CGFloat)margin {
    NSInteger count = images.count;
    CGFloat photoW = (width - (photosMaxCol - 1) * margin) / photosMaxCol;
    CGFloat photoH = photoW;
    
    CGFloat photosW = 0;
    CGFloat photosH = 0;
    NSInteger cols = photosMaxCol;
    NSInteger rows = (count + cols - 1) / cols;
    
    photosW = photoW * cols + (cols - 1) * margin;
    photosH = photoH * rows + (rows - 1) * margin;
    return CGSizeMake(photosW, photosH);
}

@end
